package org.nbile.service;

import org.nbile.commons.ResultUtils;
import org.nbile.component.AccountComponent;
import org.nbile.component.AnchorComponent;
import org.nbile.component.TransactionComponent;
import org.stellar.sdk.KeyPair;
import org.stellar.sdk.Network;
import org.stellar.sdk.Server;
import org.stellar.sdk.responses.AccountResponse.Balance;
import org.stellar.sdk.responses.SubmitTransactionResponse;

import java.util.List;

/**
 * Document Start
 * 访问Stellar网络的服务类
 * Document End
 * Author: songwenpeng@le.com
 * Time: 2016年10月3日 下午11:31:21
 */
public class NbileService {
    private Server server;
    private AccountComponent account = new AccountComponent();
    private TransactionComponent transaction = new TransactionComponent();
    private AnchorComponent anchor = new AnchorComponent();
	//区分网络
    private String netPass = "Public Global NBiLe Network 201612";
	//测试API地址
    private String apiURL = "http://api.t.nbile.com";


    /**
     * 构造方法 构造一个Service
     */
    public NbileService() {
        //使用公共网络需要做签名时添加前缀 如果不调用此方法则会报 tx_bad_auth错误
        Network.usePublicNetwork(netPass);
        //创建一个服务
        server = new Server(apiURL);
    }

    /**
     * 创建账户
     * 此时创建的账户在区块链上不存在
     * @return
     */
    public KeyPair createAccount() {
		//账户创建的方法业务逻辑
        return account.createAccount();
    }

    /**
     * 向指定账户Payment 原生资产
     * 如果账户不存在区块链网络上 则自动激活
     * @param sourceSeed 源账户
     * @param targetAccountID 发给哪个账户
     * @param amount 发送多少钱
     * @return
     */
    public int paymentNative(String sourceSeed, String targetAccountID, String amount) {
        // TODO Auto-generated method stub
		//(内)公钥私钥都有,封装了方法,
		//accounts().account()  刷新序列号
		//PaymentOperation 目标账户里面发什么类型的资产, amount金额
		//transaction  Memo.text 备注信息
		//repose 将返回往上抛出
		
		
		//getUserultCode(){}  0成功 其他返回则为其他的错误类型, -7余额不足; -9手续费太少, 少于100; -10事务签名签多了; -11其他错误
        SubmitTransactionResponse response = this.transaction.paymentXLM(server, sourceSeed, targetAccountID, amount);
        return ResultUtils.getResultCode(response);
    }

    /**
     * 获取账户余额信息
     * @param accountID
     * @return
     */
    public List<Balance> accountDetails(String accountID) {
        // TODO Auto-generated method stub
        return this.account.accountDetails(server, accountID);
    }

    /**
     * 添加信任资产
     * @param issuerCode
     * @param issuerAccountID
     * @param accountSeed
     * @param limit
     * @return
     */
    public int setTrust(String issuerCode, String issuerAccountID, String accountSeed, String limit) {
        SubmitTransactionResponse response = this.anchor.setTrust(server, issuerCode, issuerAccountID, accountSeed, limit);
        return ResultUtils.getResultCode(response);
    }

    /**
     * 支付发行资产
     * @param sourceSeed
     * @param targetAccountID
     * @param amount
     * @param issuerCode
     * @param issuerAccountID
     * @return
     */
    public int paymentAsset(String sourceSeed, String targetAccountID, String amount, String issuerCode, String issuerAccountID) {
        SubmitTransactionResponse response = this.anchor.transfer(server, sourceSeed, targetAccountID, amount, issuerCode, issuerAccountID);
        return ResultUtils.getResultCode(response);
    }

    /**
     * 设置允许持有制定账户发行的数字资产
     * @param sourceSeed 发起Transaction的源账户
     * @param targetAccountID 想持有该数字资产的目标账户
     * @param issuerCode 数字资产的发行代码标识
     * @param issuerAccountID 数字资产的发行账户ID
     * @param isAllowTrust 是否允许持有该数字资产
     * @return
     */
    public int allowTrust(String sourceSeed, String targetAccountID, String issuerCode, String issuerAccountID, boolean isAllowTrust) {
        return ResultUtils.getResultCode(this.anchor.allowTrust(server, sourceSeed, targetAccountID, issuerCode, issuerAccountID, isAllowTrust));
    }

    public Server getServer() {
        return server;
    }

}
